﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YDModel;
using YDServices.Comm;

namespace YDServices
{
    public class FactoryService : BaseService
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<T_Factory> getList(ConditionItem[] conds)
        { 
             try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_Factory.Where(o=>o.State!=false).AsQueryable();
                    if (conds != null)
                    {
                        var qb = QueryBuilder<T_Factory>.Create().AddConditions(conds);
                        list = list.Where(qb.ToExpression());
                    }
                    return list.ToList();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        public List<T_Factory_List> getComboxList(int FactoryType,string factoryName)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    List<T_Factory_List> list = db.T_Factory.Where(o => o.State != false&& o.FactoryType== FactoryType)
                        .Select(o => new T_Factory_List {ID=o.ID, FactoryName= o.FactoryName }).ToList() ; 
                    //if(!factoryName.Equals(""))
                    //{
                    T_Factory_List tf = new T_Factory_List();
                    tf.ID = 0;
                    tf.FactoryName = factoryName;
                    list.Add(tf);
                   // }

                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        public List<T_Factory_List> getComboxList(int FactoryType)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    List<T_Factory_List> list = db.T_Factory.Where(o => o.State != false && o.FactoryType == FactoryType)
                        .Select(o => new T_Factory_List { ID = o.ID, FactoryName = o.FactoryName }).ToList();
 
                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        public List<T_Factory_List> getComboxList( string factoryName = "")
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    List<T_Factory_List> list = db.T_Factory.Where(o => o.State != false)
                        .Select(o => new T_Factory_List { ID = o.ID, FactoryName = o.FactoryName }).ToList();
                    if (!factoryName.Equals(""))
                    {
                        T_Factory_List tf = new T_Factory_List();
                        tf.ID = 0;
                        tf.FactoryName = factoryName;
                        list.Add(tf);
                    }

                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tu"></param>
        public void AddItem(T_Factory tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var user = db.T_Factory.Where(o => o.FactoryName == tu.FactoryName && o.State == true).FirstOrDefault();
                    if (user != null)
                    {
                        throw new ApplicationException("厂家已存在!");
                    }

                    tu.State = true;
                    tu.CreatedTime = GetSqlNow();
                    tu.CreatedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.T_Factory.Add(tu);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_Factory tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var item = db.T_Factory.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                    if (item == null)
                    {
                        throw new ApplicationException("厂家信息不存在!");
                    }
                    item.FactoryName = tu.FactoryName;
                    item.Address = tu.Address;
                    item.FactoryType = tu.FactoryType;
                    item.ModifiedTime = GetSqlNow();
                    item.ModifiedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginUser"></param>
        public void DelItem(T_Factory item)  //List<T_Factory> tuList 
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    //foreach (var item in tuList)
                    //{
                        var fact = db.T_Factory.Where(o => o.ID == item.ID ).FirstOrDefault();
                        if (fact == null)
                        {
                            throw new ApplicationException("厂家信息不存在!");
                        }
                        fact.State = false;
                        fact.DeletedTime = GetSqlNow();
                        fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    //}
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }

        //public void saveImport(DataTable dt, T_User loginUser)
        //{
        //    try
        //    {
        //        using (YDDataEntities db = new YDDataEntities())
        //        {
        //            foreach (DataRow drr in dt.Rows)
        //            {

        //                if (!drr[0].ToString().Equals("") && !drr[1].ToString().Equals(""))
        //                {
        //                    string itemCode = drr[0].ToString();
        //                    var item = db.T_Factory.Where(o => o.ItemCode.Equals(itemCode) && o.State == true).FirstOrDefault();
        //                    if (item != null)  //如果存在,则更新
        //                    {
        //                        item.ItemName = drr[1].ToString();
        //                        item.Spec = drr[2].ToString();
        //                        item.Unit = drr[3].ToString();
        //                        item.ItemState = 1;

        //                        item.ModifiedOn= GetSqlNow();
        //                        item.ModifiedBy = loginUser.UserCode;
        //                        item.ModifiedByName = loginUser.UserName;

        //                        db.SaveChanges();
        //                    }
        //                    else
        //                    {
        //                        T_Factory newItem = new T_Factory();
        //                        newItem.ItemCode = drr[0].ToString();
        //                        newItem.ItemName = drr[1].ToString();
        //                        newItem.Spec = drr[2].ToString();
        //                        newItem.Unit = drr[3].ToString();
        //                        newItem.ItemState = 1;

        //                        newItem.CreateOn = GetSqlNow();
        //                        newItem.CreateBy = loginUser.UserCode;
        //                        newItem.CreateByName = loginUser.UserName;
        //                        newItem.State = true;

        //                        db.T_Factory.Add(newItem);
        //                        db.SaveChanges();
        //                    }
        //                }

        //            } 

        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Errors.Add(ex);
        //        WriteException(ex);
        //    }
        //}



    }
}
